package com.whale.cloud.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import lombok.Data;

/** Created by Abel on 2017/6/13. */
@Data
@Component
public class ValueOps {

  @Autowired private RedisTemplate<Object, Object> redisTemplate;
  @Autowired private StringRedisTemplate stringRedisTemplate;





  /**
   * 设置 String 型值，并设置失效时长为 secs 秒
   *
   * @param key
   * @param value
   * @param secs
   * @return
   */
  public boolean setValueStringWithExpire(String key, String value, Long secs) {
    ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
    valueOperations.set(key, value);
    stringRedisTemplate.expire(key, secs, TimeUnit.SECONDS);
    return true;
  }

  /**
   * 设置 String 型值，并在设定的 expireData 时间失效
   *
   * @param key
   * @param value
   * @param expireData
   * @return
   */
  public boolean setValueStringExpireAt(String key, String value, Date expireData) {
    ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
    valueOperations.set(key, value);
    stringRedisTemplate.expireAt(key, expireData);
    return true;
  }

  /**
   * 设置 String 型值
   *
   * @param key
   * @return
   */
  public boolean setValueString(String key, String value) {
    ValueOperations<String, String> stringValueOperations = stringRedisTemplate.opsForValue();
    stringValueOperations.set(key, value);
    return true;
  }

  /**
   * 获取 String 型值
   *
   * @param key
   * @return
   */
  public String getValueString(String key) {
    ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
    return valueOperations.get(key);
  }

  /**
   * 设置 Object 类型值，并设置失效时长为 secs 秒
   *
   * @param key
   * @param object
   * @param secs
   */
  public boolean setValueObjectWithExpire(String key, Object object, Long secs) {
    ValueOperations<Object, Object> objectValueOperations = redisTemplate.opsForValue();
    objectValueOperations.set(key, object);
    redisTemplate.expire(key, secs, TimeUnit.SECONDS);
    return true;
  }

  /**
   * 设置 Object 类型值，并在设定的 expireData 时间失效
   *
   * @param key
   * @param object
   * @param expireData
   */
  public boolean setValueObjectExpireAt(String key, Object object, Date expireData) {
    ValueOperations<Object, Object> objectValueOperations = redisTemplate.opsForValue();
    objectValueOperations.set(key, object);
    redisTemplate.expireAt(key, expireData);
    return true;
  }

  /**
   * 获取 Object 类型值
   *
   * @param key
   */
  public Object getValueObject(String key) {
    ValueOperations<Object, Object> objectValueOperations = redisTemplate.opsForValue();
    return objectValueOperations.get(key);
  }
}
